package org.vehicle.interceptor;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.vehicle.iface.DeviceLoginController;
import org.vehicle.service.redis.RedisService;

public class InterfaceTokenInterceptor extends HandlerInterceptorAdapter{
	private static Logger logger = LogManager.getLogger(DeviceLoginController.class.getName());
	@Autowired
	private RedisService redisService;
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		try{
			String token=request.getParameter("Token");
			if(token!=null){
				String info=redisService.getString(token);
	 	    	if(info!=null){
	 	    		return super.preHandle(request, response, handler);
	 	    	}
			}
			PrintWriter pw=response.getWriter();
		    pw.write("{\"state\":0,\"mes\":\"token不存在，请重新登录\"}");
		}catch(Exception e){
			logger.error(e);
		}
		return false;
	}
	
}
